package com.example.xui;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;

import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.Toast;
import android.widget.VideoView;

import com.example.xui.Way.GlideEngine;
import com.example.xui.Way.changStatusIconCollor;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.config.PictureMimeType;
import com.luck.picture.lib.entity.LocalMedia;
import com.xuexiang.xui.XUI;
import com.xuexiang.xui.widget.edittext.MultiLineEditText;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class Article extends AppCompatActivity {
    private ImageView add_Picture;
    private ImageView add_Vedio;
    private ImageView picture;
    private VideoView videoView;
    private String stdata;
    private String sno;
    private FloatingActionButton floatingActionButton;
    private MultiLineEditText multiLineEditText;
    private String content;
    private String vedioText;
    private String imageText;
    private  PictureSelector pictureSelector;
    private PictureSelector pictureSelector1;
    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        XUI.initTheme(this);
        setContentView(R.layout.activity_article);
        Window window =this.getWindow();
        window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
        window.setStatusBarColor(ContextCompat.getColor(this,R.color.colorPrimaryDark));
        changStatusIconCollor changStatusIconCollor=new changStatusIconCollor();
        changStatusIconCollor.changStatusIconCollor1(window,true);
        init();
    }

    public void init(){
        sno=getIntent().getStringExtra("sno");
        multiLineEditText=findViewById(R.id.multiLineEditText1);
        Log.i("tag","tag12="+content);
        add_Picture=findViewById(R.id.add_picture);
        add_Vedio=findViewById(R.id.add_vedio);
        picture=findViewById(R.id.picture);
        videoView=findViewById(R.id.vedio);
        floatingActionButton=findViewById(R.id.add_note1);
        floatingActionButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                content=multiLineEditText.getContentText();
                SimpleDateFormat formatter=new SimpleDateFormat   ("yyyy-MM-dd HH:mm:ss");
                Date curDate =  new Date(System.currentTimeMillis());
                stdata=formatter.format(curDate);
                add_tie(sno,content,imageText,vedioText,stdata);
                finish();
            }
        });
    }

    public void Image_add(View v){
        pictureSelector= PictureSelector.create(Article.this);
        pictureSelector.openGallery(PictureMimeType.ofImage())
                .loadImageEngine(GlideEngine.createGlideEngine()) // 请参考Demo GlideEngine.java
                .forResult(PictureConfig.CHOOSE_REQUEST);
    }

    public void Vedio_add(View v){
        pictureSelector1= PictureSelector.create(Article.this);
        pictureSelector1.openGallery(PictureMimeType.ofVideo())
                .loadImageEngine(GlideEngine.createGlideEngine()) // 请参考Demo GlideEngine.java
                .forResult(PictureConfig.LOADED);
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            Log.i("tag","test123="+requestCode);
            switch (requestCode) {
                case PictureConfig.CHOOSE_REQUEST:
                    List<LocalMedia> selectList = pictureSelector.obtainMultipleResult(data);
                    for(int i=0;i<selectList.size();i++) {
                    /*    Log.i("tag","test123="+2);
                        Log.i("tag","test123="+selectList.get(i).getPath());*/
                        imageText=selectList.get(i).getPath();
                        picture.setImageURI(Uri.parse(selectList.get(i).getPath()));
                        picture.setVisibility(View.VISIBLE);
                    }
                    break;
                case PictureConfig.LOADED:
                    List<LocalMedia> selectList1 = pictureSelector1.obtainMultipleResult(data);
                    for(int i=0;i<selectList1.size();i++) {
                        videoView.setVideoURI(Uri.parse(selectList1.get(i).getPath()));
                        videoView.start();
                        videoView.setVisibility(View.VISIBLE);
                        vedioText=selectList1.get(i).getPath();
                    }
                    break;
                default:
                    break;

            }
        }
    }

    public void add_tie(String userId,String connect,String image,String vedio,String data){
        new Thread(){
            @Override
            public void run() {
                super.run();
                RequestParams params = new RequestParams("http://10.138.87.49:8080/invitation/invitationinsert");
                params.addHeader("Content-Type", "application/x-www-form-urlencoded");
                params.addBodyParameter("invitationUserID",userId);
                params.addBodyParameter("invitationContent",connect);
                params.addBodyParameter("invitationData",data);
                params.addBodyParameter("invitationPhoto",image);
                params.addBodyParameter("invitationTv",vedio);
                x.http().post(params, new Callback.CacheCallback<String>() {
                    @Override
                    public void onSuccess(String result) {
                            if(result.equals("success")){
                                Toast.makeText(Article.this,"帖子发布成功",Toast.LENGTH_SHORT).show();
                                Log.i("tag","tag=success");
                            }
                            else{
                                Toast.makeText(Article.this,"帖子发布失败",Toast.LENGTH_SHORT).show();
                                Log.i("tag","tag=failed");
                            }

                    }

                    @Override
                    public void onError(Throwable ex, boolean isOnCallback) {
                        Toast.makeText(Article.this,"网络请求错误",Toast.LENGTH_SHORT).show();
                        Log.i("tag","tag=error");
                    }

                    @Override
                    public void onCancelled(CancelledException cex) {
                        Log.i("tag","tag=cancelled");
                    }

                    @Override
                    public void onFinished() {
                        Log.i("tag","tag=finish");
                    }

                    @Override
                    public boolean onCache(String result) {
                        return false;
                    }
                });
            }
        }.start();
    }

}
